/**
 * Created by meiyun1 on 2017/3/8.
 */
define(['angular','dateRangPicker'],function (angular,dateRangPicker) {
    angular.module('directives', []).directive('fileUpload',[function () {
        return {
            restrict:'EA',
            link:function (scop,iElement,iAttrs) {
                $_self = $(iElement);
                var initialPreview = [];
                var initialPreviewConfig = [];
                var previewImage = $('input[name="'+$_self.attr('data-for')+'"]').attr('data-preview')||$('input[name="'+$_self.attr('data-for')+'"]').val();
                if(previewImage){
                    initialPreview.push(previewImage);
                    initialPreviewConfig.push({key:getFileName(previewImage)})
                }
                $_self.fileinput({
                    //allowedFileTypes:['image'],
                    language:'zh',
                    initialPreview:initialPreview,
                    initialPreviewConfig:initialPreviewConfig,
                    deleteUrl:'',
                    initialPreviewAsData: true,
                    multiple:false,
                    allowedFileExtensions:['jpg','jpeg','png','gif','ico','bmp'],
                    uploadUrl: "/tmp/upload",
                    overwriteInitial:true,
                    dropZoneEnabled:false
                }).on('fileuploaded', function(event, data, previewId, index) {
                    if (data.response.code == 0){
                        $('#'+previewId).attr('data-code',getFileName(data.response.result.attachment));
                        $('input[name="'+$(event.target).attr('data-for')+'"]').val(data.response.result.original);
                    }else {
                        utils.showNotification('上传文件失败',3);
                    }

                }).on('filecleared',function(event){
                    //console.log(event,1);
                    $('input[name="'+$(event.target).attr('data-for')+'"]').val('');
                }).on('filedeleted',function(event,key){
                    //console.log(event,key,2);
                    $('input[name="'+$(event.target).attr('data-for')+'"]').val('');
                }).on('filereset',function(event){
                    // console.log(event,3);
                    $('input[name="'+$(event.target).attr('data-for')+'"]').val('');
                }).on('filesuccessremove',function(event,id){
                    $('input[name="'+$(event.target).attr('data-for')+'"]').val('');
                });
            }
        }
    }]).directive('dateTimePicker',[function () {
        return {
            restrict:'EA',
            link:function (scop,iElement,iAttrs) {
                $(iElement).datetimepicker({
                    format:'YYYY-MM-DD HH:mm',
                    locale:'zh-cn',
                    icons: {
                        time: "fa fa-clock-o",
                        date: "fa fa-calendar",
                        up: "fa fa-chevron-up",
                        down: "fa fa-chevron-down",
                        previous: 'fa fa-chevron-left',
                        next: 'fa fa-chevron-right',
                        today: 'fa fa-screenshot',
                        clear: 'fa fa-trash',
                        close: 'fa fa-remove'
                    }
                });
            }
        };

    }]).directive('tooltip',[function () {
        return {
            restrict:'EA',
            link:function (scop,iElement,iAttrs) {
                $(iElement).tooltip({
                    placement:'top',
                    trigger:'focus',
                    title:iAttrs['title']
                });
            }
        }
    }]).directive('bootstrapSwitch',['$rootScope',function ($rootScope) {
        return {
            restrict:'EA',
            link:function (scope,iElement,iAttrs) {
                $(iElement).bootstrapSwitch();
                var ischecked = iAttrs['ischecked'];
                if (ischecked == 1){
                    $(iElement).bootstrapSwitch('setState',true);
                }
                $(iElement).on('switch-change', function (e, data) {
                    scope.$emit('bootstrap-switch-change',data);
                });
            }
        }
    }]).directive('dateRangePicker',[function () {
        return {
            restrict:'EA',
            link:function (scope,iElement,iAttrs) {
                var start = iAttrs['start']||moment().subtract(6,'days');
                var end = iAttrs['end']||moment();
                var category =iAttrs['category']||'';
                new dateRangPicker($(iElement),{
                    startDate:start,
                    endDate:end,
                    locale: {
                        applyLabel: "确认",
                        cancelLabel: "取消",
                        fromLabel: "从",
                        toLabel: "至",
                        customRangeLabel: "自定义",
                        separator: " 至 ",
                        format: "YYYY-MM-DD",
                        monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
                        daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"]
                    },
                    ranges: {
                        "今天": [moment(), moment().add(1,'days')],
                        "昨天": [moment().subtract(1, "days"), moment().subtract(1, "days")],
                        "过去7天": [moment().subtract(6, "days"), moment().add(1,'days')],
                        "过去30天": [moment().subtract(29, "days"), moment().add(1,'days')],
                        "本月": [moment().startOf("month"), moment().endOf("month")],
                        "上个月": [moment().subtract(1, "month").startOf("month"), moment().subtract(1, "month").endOf("month")]
                    },
                },function (start, end, label) {
                    scope.$emit('date-range-select',{start:start,end:end,category:category});
                });

            }
        };
    }]).directive('stringToNumber', function() {
        return {
            require: 'ngModel',
            link: function(scope, element, attrs, ngModel) {
                ngModel.$parsers.push(function(value) {
                    return '' + value;
                });
                ngModel.$formatters.push(function(value) {
                    return parseInt(value);
                });
            }
        };
    })
})